/**
 * 初始化用户管理详情对话框
 */
var VpnUserInfoInfoDlg = {
    vpnUserInfoInfoData: {}
};

/**
 * 清除数据
 */
VpnUserInfoInfoDlg.clearData = function () {
    this.vpnUserInfoInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
VpnUserInfoInfoDlg.set = function (key, val) {
    this.vpnUserInfoInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
VpnUserInfoInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
VpnUserInfoInfoDlg.close = function () {
    parent.layer.close(window.parent.VpnUserInfo.layerIndex);
}

/**
 * 收集数据
 */
VpnUserInfoInfoDlg.collectData = function () {
    this
        .set('id')
        .set('userName')
        .set('regVersion')
        .set('regChannel')
        .set('curPlatform')
        .set('status')
        .set('ipAddr')
        .set('regTime')
        .set('lastOnline')
        .set('lastPaymentChannel')
        .set('paymentNum')
        .set('packageName')
        .set('beginTime')
        .set('endTime');
}

/**
 * 提交添加
 */
VpnUserInfoInfoDlg.addSubmit = function () {

    // this.clearData();
    // this.collectData();
    //
    // //提交信息
    // var ajax = new $ax(Feng.ctxPath + "/vpnUserInfo/add", function (data) {
    //     Feng.success("添加成功!");
    //     window.parent.VpnUserInfo.table.refresh();
    //     VpnUserInfoInfoDlg.close();
    // }, function (data) {
    //     Feng.error("添加失败!" + data.responseJSON.message + "!");
    // });
    // ajax.set(this.vpnUserInfoInfoData);
    // ajax.start();
}

/**
 * 重置用户密码
 */
VpnUserInfoInfoDlg.resetPwd = function(){
    var userName = $("#username").val();
    var password = $("#password").val();
    var password2 = $("#password2").val();
    var loading;

    if(userName == '' || userName == null){
        layer.msg("用户名不能为空")
    }
    else if(password == '' || password == null){
        layer.msg("密码不能为空")
    }
    else if(password !== password2){
        layer.msg("两次输入密码不一致")
    }
    else{

        layer.confirm("是否要为用户重置密码?",function (index) {
            layer.close(index);
            loading = layer.load(0,{
                shade:false,
                time: 30 * 1000
            });
            setTimeout(function () {
                //提交信息
                var ajax = new $ax(Feng.ctxPath + "/vpnUserInfo/resetPwd", function (data) {
                    Feng.success("重置密码成功!");
                    layer.close(loading);
                    VpnUserInfoInfoDlg.close();
                }, function (data) {
                    layer.close(loading);
                    Feng.error("重置密码失败!" + data.responseJSON.message + "!");
                });
                ajax.set("username",userName);
                ajax.set("pwd",password);
                ajax.start();
            })
        });
    }
}

/**
 * 禁用用户账号
 */
VpnUserInfoInfoDlg.disableUser = function(){
    var userName = $("#username").val();
    var status = $("#status").val();

    var loading;
    var content = "是否禁用此账号?";
    if (status == 1){
        content = "是否启用此账号?"
    }else{
        content = "是否禁用此账号?"
    }

    if(userName == '' || userName == null){
        layer.msg("用户名不能为空")
    }
    else{
        layer.confirm(content,function (index) {
            layer.close(index);
            loading = layer.load(0,{
                shade:false,
                time: 30 * 1000
            });
            setTimeout(function () {
                //提交信息
                var ajax = new $ax(Feng.ctxPath + "/vpnUserInfo/disableUser", function (data) {
                    Feng.success("操作成功!");
                    layer.close(loading);
                    VpnUserInfoInfoDlg.close();
                }, function (data) {
                    layer.close(loading);
                    Feng.error("操作失败!" + data.responseJSON.message + "!");
                });
                ajax.set("username",userName);
                ajax.set("status",status);
                ajax.start();
            })
        });
    }
}

/**
 * 提交修改
 */
VpnUserInfoInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/vpnUserInfo/update", function (data) {
        Feng.success("修改成功!");
        window.parent.VpnUserInfo.table.refresh();
        VpnUserInfoInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.vpnUserInfoInfoData);
    ajax.start();
};

/**
 * 添加VIP套餐
 */
VpnUserInfoInfoDlg.addVipPackage = function(){
    var userName = $("#username").val();
    var packageId = $("#selectPackage").val();
    var loading;

    if(userName == '' || userName == null){
        layer.msg("用户名不能为空")
    }
    else if(packageId == '' || packageId == null){
        layer.msg("请选择套餐")
    }
    else{

        layer.confirm("是否要为用户添加VIP套餐?",function (index) {
            layer.close(index);
            loading = layer.load(0,{
                shade:false,
                time: 30 * 1000
            });
            setTimeout(function () {
                //提交信息
                var ajax = new $ax(Feng.ctxPath + "/vpnUserInfo/addVipPackage", function (data) {
                    Feng.success("添加VIP用户套餐成功!");
                    layer.close(loading);
                    VpnUserInfoInfoDlg.close();
                }, function (data) {
                    Feng.error("设置套餐失败!" + data.responseJSON.message + "!");
                    layer.close(loading);
                });
                ajax.set("username",userName);
                ajax.set("packageId",packageId);
                ajax.start();
            })
        });
    }
};

/**
 * 获取选择套餐的列表数据
 */
VpnUserInfoInfoDlg.selectPackageList = function(){
    var ajax = new $ax(Feng.ctxPath + "/vpnUserInfo/getFilterData", function (data) {

        var packageList = data.packageList;
        packageList.forEach(function (item) {
            $("#selectPackage").append("'<option value='"+item.pid+"'>"+item.pname+"</option>'");
        }) ;

    }, function (data) {
        Feng.error("套餐列表获取失败" + data.responseJSON.message + "!");
    });
    ajax.start();
};

$(function () {
    VpnUserInfoInfoDlg.selectPackageList();
});
